package com.wooribank.android.tablet.accountCheck;

import java.util.ArrayList;

import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.wooribank.android.tablet.Account;
import com.wooribank.android.tablet.AccountBox;
import com.wooribank.android.tablet.MyApplication;
import com.wooribank.android.tablet.R;
import com.wooribank.android.tablet.common.JsonManager;

public class DepositFragment extends Fragment {
	private View mContentView;
	private TableLayout tableLayout;
	private View selectedView;
	private int currentDetailViewIndex = -1;
	private Handler handler;
	private ProgressBar progressBar;
	private View content;

	private static final String URL_GET_ACCOUNTS = "com.wooribank.tablet.DepositAccountCheckActivity.URL_GET_ACCOUNTS";

	private OnClickListener accountBoxClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			int selectedTableRowIndex = tableLayout.indexOfChild((TableRow) v.getParent());
			if (selectedView == null || selectedView != v) {
				selectedView = v;
			} else {
				selectedView = null;
			}
			toggleDetail(selectedTableRowIndex);
		}
	};

	private Callback handlerCallback = new Callback() {
		@Override
		public boolean handleMessage(Message msg) {
			switch (msg.what) {
			case MyApplication.MSG_GET_DATA:
				progressBar.setVisibility(View.GONE);
				content.setVisibility(View.VISIBLE);
				Bundle bundle = msg.getData();
				ArrayList<Account> accounts = bundle.getParcelableArrayList(MyApplication.KEY_GET_DATA);
				TableRow.LayoutParams rowParams = new TableRow.LayoutParams();
				TableRow tableRow = new TableRow(getActivity());
				AccountBox box1 = new AccountBox(getActivity(), accounts.get(0));
				box1.setOnClickListener(accountBoxClickListener);
				tableRow.addView(box1, rowParams);
				AccountBox box2 = new AccountBox(getActivity(), accounts.get(1));
				box2.setOnClickListener(accountBoxClickListener);
				tableRow.addView(box2, rowParams);
				AccountBox box3 = new AccountBox(getActivity(), accounts.get(2));
				box3.setOnClickListener(accountBoxClickListener);
				tableRow.addView(box3, rowParams);
				tableLayout.addView(tableRow, new TableLayout.LayoutParams());
			}
			return false;
		}

	};

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		mContentView = inflater.inflate(R.layout.account_check_deposit_fragment, null);
		content = mContentView.findViewById(R.id.content);
		progressBar = (ProgressBar) mContentView.findViewById(R.id.progress);
		return mContentView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		tableLayout = (TableLayout) mContentView.findViewById(R.id.tableLayout1);
		handler = new Handler(handlerCallback);
		getAccounts();
		super.onActivityCreated(savedInstanceState);
	}

	private void getAccounts() {
		JsonManager jsonManager = JsonManager.getInstance();
		jsonManager.getAccounts(URL_GET_ACCOUNTS, handler);

	}

	private void toggleDetail(int selectedTableRowIndex) {
		if (selectedView == null) {
			tableLayout.removeViewAt(selectedTableRowIndex + 1);
			currentDetailViewIndex = -1;
		} else {
			if (currentDetailViewIndex != -1) {
				tableLayout.removeViewAt(currentDetailViewIndex);
				if (selectedTableRowIndex > currentDetailViewIndex)
					selectedTableRowIndex -= 1;
			}
			TableRow tableRow = new TableRow(getActivity());
			LayoutInflater inflater = LayoutInflater.from(getActivity());
			LinearLayout detailLinearLayout = (LinearLayout) inflater.inflate(R.layout.account_detail,
					tableRow, false);
			AccountBox accountBox = (AccountBox) selectedView;
			((TextView) detailLinearLayout.findViewById(R.id.textView1)).setText(accountBox.getAccount()
					.getName());
			TableRow.LayoutParams rowParams = new TableRow.LayoutParams(detailLinearLayout.getLayoutParams());
			rowParams.span = 3;
			tableRow.addView(detailLinearLayout, rowParams);
			tableLayout.addView(tableRow, selectedTableRowIndex + 1, new TableRow.LayoutParams());
			currentDetailViewIndex = selectedTableRowIndex + 1;
		}

	}
}
